<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Booth Product Categories Controller
 *
 * @category
 * @package   
 * @author     Phạm Ngọc Khánh <phamngockhanh262@gmail.com>
 */
class Booth_product_categories extends CI_Controller 
{	
	private $_data;
		
  	function __construct()
  	{
  		parent::__construct();
  	    $this->load->model('booth_product_categories_model');
  	}
  	  	
  	
	public function index($boothId, $page = 1)
	{   
	    $this->load->library('pagination');
        $data = array('boothId' => $boothId);
	    
        $cnt = $this->booth_product_categories_model->cnt($boothId);
        
        $config = array(
          'base_url' => base_url("booth_product_categories/index/{$boothId}"),
          'total_rows' => $cnt['cnt'],
          'per_page' => 6,
          'num_links' => 6,
          'uri_segment' => 4,
          'use_page_numbers' => TRUE,
          'cur_tag_open'  => '<li class="active"><span>',
          'cur_tag_close' => '</span></li>'    
        );
        $config['num_tag_open']  = $config['prev_tag_open']  = $config['next_tag_open']  = $config['last_tag_open'] = $config['first_tag_open'] = '<li>';
        $config['num_tag_close'] = $config['prev_tag_close'] = $config['next_tag_close'] = $config['last_tag_close'] = $config['first_tag_close'] ='</li>';
           
        $this->pagination->initialize($config);
        
        $start = --$page * $config['per_page'];
        $params = array(
            'start' => $start, 
            'limit' => $config['per_page'],
            'where'  => " AND `booth_id` = {$boothId}"
        );
        
        $data['boothProductCategories'] = $this->booth_product_categories_model->getListBoothProductCategories($params);
	    
        $this->_data['content'] = $this->load->view('booth/ProductCategories/Index', $data, TRUE);
        $this->load->view('layout/middleoffice/index', $this->_data);
	}	
	
	public function add($boothId = NULL, $id = NULL)
	{
	    $this->edit($boothId, $id);
	}
	
	public function edit($boothId, $id)
	{   
	    $this->load->library(array('form_validation'));

	    $id = (int) $id;
	    $boothId = (int) $boothId;
	    
	    $data = array('boothId' => $boothId);
	    $data['parentBoothProductCategories'] = $this->booth_product_categories_model->getListBoothProductCategories(array('parent' => TRUE,
	                                                                                                                       'where'  => " AND `booth_id` = {$boothId}"));
	    
	    $this->form_validation->set_rules('name', '', 'trim|required');
	    $this->form_validation->set_rules('parent_id', '', 'trim');
	    
	    if ($id) {
            $data['boothProductCategories'] = $this->booth_product_categories_model->getBoothProductCategoriesById($id);
        }
	    
	    if ($this->form_validation->run() == FALSE) {	        
	        $this->_data['content'] = $this->load->view("booth/ProductCategories/Add", $data, TRUE);
	        $this->load->view('layout/middleoffice/index', $this->_data);
	        
	        return FALSE;
	    } else {
            if (isset($_POST['parent_id']) && $_POST['parent_id'] == 0) {
                unset($_POST['parent_id']);
            }
	        
	        if ($id) {
	            $this->booth_product_categories_model->update($id, $_POST);
	            $this->session->set_flashdata('message', 'Đã thay đổi thành công!');
	        } else {
	            $_POST['booth_id'] = (int) $boothId;
    	        $this->booth_product_categories_model->create($_POST);
    	        $this->session->set_flashdata('message', 'Đã thêm mới thành công!');
	        }
	        
	        redirect("booth_product_categories/index/{$boothId}");
    	    return TRUE;
	    }
	}
	
	public function remove($boothId, $id)
	{
	    $id = (int) $id;
	    $boothId = (int) $boothId;
	    
	    $this->booth_product_categories_model->delete($id);
	    $this->session->set_flashdata('message', 'Đã xóa thành công!');
	    
  	    redirect("booth_product_categories/index/{$boothId}");
	}

}